package Frames;

import DAO.Dao;
import Entidades.Emissor;
import Entidades.Entrada;
import Entidades.TipoEntrada;
import Entidades.Usuario;
import Utils.S20BinaryLookup;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Davidson
 */
public class IfrmManterEntrada extends javax.swing.JInternalFrame{

    Dao dao = new Dao();
    List<TipoEntrada> tipoEntrada = dao.listarTipoEntrada();
    List<Emissor> emissores = dao.listarEmissores();
    List<Usuario> usuarios = dao.listarUsuarios();
    List<Entrada> entradas;
    Usuario usuario;
    
    public IfrmManterEntrada(Usuario usuario){
        initComponents();
        this.usuario = usuario;
        lbUsuario.setText(usuario.getNome());
        try {
            carregaTipoEntradas();
            carregaEmissores();
            carregaUsuarios();
        } catch (SQLException ex) {
            Logger.getLogger(IfrmManterEntrada.class.getName()).log(Level.SEVERE, null, ex);
        }
        new S20BinaryLookup(cbEmissores);        
    }    
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bgSituacao = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        cbEmissores = new javax.swing.JComboBox();
        cbTipoEntrada = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        btConsultarEntrada = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        ftfDtRegistroIni = new javax.swing.JFormattedTextField();
        imgDataLancamentoIni = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        ftfDtRegistroFim = new javax.swing.JFormattedTextField();
        imgDataLancamentoFim = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        ftfDtReferenciaIni = new javax.swing.JFormattedTextField();
        imgDataReferenciaIni = new javax.swing.JLabel();
        ftfDtReferenciaFim = new javax.swing.JFormattedTextField();
        jLabel10 = new javax.swing.JLabel();
        imgDataReferenciaFim = new javax.swing.JLabel();
        cbUsuario = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        ckDtRegistro = new javax.swing.JCheckBox();
        ckDtReferencia = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        lblValorTotal = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        txtUsuario = new javax.swing.JLabel();
        lbUsuario = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbEntradas = new javax.swing.JTable();

        setClosable(true);
        setFrameIcon(null);
        setMinimumSize(new java.awt.Dimension(758, 361));

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        cbEmissores.setEditable(true);
        cbEmissores.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        cbEmissores.setForeground(new java.awt.Color(41, 41, 41));
        cbEmissores.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos" }));
        cbEmissores.setMaximumSize(new java.awt.Dimension(250, 30));
        cbEmissores.setMinimumSize(new java.awt.Dimension(250, 30));
        cbEmissores.setPreferredSize(new java.awt.Dimension(250, 30));

        cbTipoEntrada.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        cbTipoEntrada.setForeground(new java.awt.Color(41, 41, 41));
        cbTipoEntrada.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos" }));
        cbTipoEntrada.setMaximumSize(new java.awt.Dimension(180, 30));
        cbTipoEntrada.setMinimumSize(new java.awt.Dimension(180, 30));
        cbTipoEntrada.setPreferredSize(new java.awt.Dimension(180, 30));

        jLabel4.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(51, 51, 51));
        jLabel4.setText("Tipo Entrada:");

        jLabel5.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(51, 51, 51));
        jLabel5.setText("Emissor:");

        btConsultarEntrada.setFont(new java.awt.Font("Calibri", 1, 14)); // NOI18N
        btConsultarEntrada.setText("Consultar");
        btConsultarEntrada.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btConsultarEntradaActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(51, 51, 51));
        jLabel2.setText("de:");

        ftfDtRegistroIni.setEditable(false);
        ftfDtRegistroIni.setBackground(new java.awt.Color(255, 255, 255));
        ftfDtRegistroIni.setForeground(new java.awt.Color(102, 102, 102));
        ftfDtRegistroIni.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));
        ftfDtRegistroIni.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        ftfDtRegistroIni.setEnabled(false);
        ftfDtRegistroIni.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        ftfDtRegistroIni.setMaximumSize(new java.awt.Dimension(126, 30));
        ftfDtRegistroIni.setMinimumSize(new java.awt.Dimension(126, 30));
        ftfDtRegistroIni.setPreferredSize(new java.awt.Dimension(126, 30));

        imgDataLancamentoIni.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/calendar.png"))); // NOI18N
        imgDataLancamentoIni.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        imgDataLancamentoIni.setEnabled(false);
        imgDataLancamentoIni.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                imgDataLancamentoIniMouseClicked(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(51, 51, 51));
        jLabel3.setText("até:");

        ftfDtRegistroFim.setEditable(false);
        ftfDtRegistroFim.setBackground(new java.awt.Color(255, 255, 255));
        ftfDtRegistroFim.setForeground(new java.awt.Color(102, 102, 102));
        ftfDtRegistroFim.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));
        ftfDtRegistroFim.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        ftfDtRegistroFim.setEnabled(false);
        ftfDtRegistroFim.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        ftfDtRegistroFim.setMargin(new java.awt.Insets(0, 0, 0, 0));
        ftfDtRegistroFim.setMaximumSize(new java.awt.Dimension(126, 30));
        ftfDtRegistroFim.setMinimumSize(new java.awt.Dimension(126, 30));
        ftfDtRegistroFim.setPreferredSize(new java.awt.Dimension(126, 30));

        imgDataLancamentoFim.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/calendar.png"))); // NOI18N
        imgDataLancamentoFim.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        imgDataLancamentoFim.setEnabled(false);
        imgDataLancamentoFim.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                imgDataLancamentoFimMouseClicked(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(51, 51, 51));
        jLabel9.setText("de:");

        ftfDtReferenciaIni.setEditable(false);
        ftfDtReferenciaIni.setBackground(new java.awt.Color(255, 255, 255));
        ftfDtReferenciaIni.setForeground(new java.awt.Color(102, 102, 102));
        ftfDtReferenciaIni.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));
        ftfDtReferenciaIni.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        ftfDtReferenciaIni.setEnabled(false);
        ftfDtReferenciaIni.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        ftfDtReferenciaIni.setMaximumSize(new java.awt.Dimension(126, 30));
        ftfDtReferenciaIni.setMinimumSize(new java.awt.Dimension(126, 30));
        ftfDtReferenciaIni.setPreferredSize(new java.awt.Dimension(126, 30));

        imgDataReferenciaIni.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/calendar.png"))); // NOI18N
        imgDataReferenciaIni.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        imgDataReferenciaIni.setEnabled(false);
        imgDataReferenciaIni.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                imgDataReferenciaIniMouseClicked(evt);
            }
        });

        ftfDtReferenciaFim.setEditable(false);
        ftfDtReferenciaFim.setBackground(new java.awt.Color(255, 255, 255));
        ftfDtReferenciaFim.setForeground(new java.awt.Color(102, 102, 102));
        ftfDtReferenciaFim.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter()));
        ftfDtReferenciaFim.setHorizontalAlignment(javax.swing.JTextField.CENTER);
        ftfDtReferenciaFim.setEnabled(false);
        ftfDtReferenciaFim.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        ftfDtReferenciaFim.setMargin(new java.awt.Insets(0, 0, 0, 0));
        ftfDtReferenciaFim.setMaximumSize(new java.awt.Dimension(126, 30));
        ftfDtReferenciaFim.setMinimumSize(new java.awt.Dimension(126, 30));
        ftfDtReferenciaFim.setPreferredSize(new java.awt.Dimension(126, 30));

        jLabel10.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(51, 51, 51));
        jLabel10.setText("até:");

        imgDataReferenciaFim.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Imagens/calendar.png"))); // NOI18N
        imgDataReferenciaFim.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        imgDataReferenciaFim.setEnabled(false);
        imgDataReferenciaFim.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                imgDataReferenciaFimMouseClicked(evt);
            }
        });

        cbUsuario.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        cbUsuario.setForeground(new java.awt.Color(41, 41, 41));
        cbUsuario.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Todos" }));
        cbUsuario.setMaximumSize(new java.awt.Dimension(180, 30));
        cbUsuario.setMinimumSize(new java.awt.Dimension(180, 30));
        cbUsuario.setPreferredSize(new java.awt.Dimension(180, 30));

        jLabel7.setFont(new java.awt.Font("Calibri", 0, 12)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(51, 51, 51));
        jLabel7.setText("Usuario:");

        ckDtRegistro.setText("Data do Registro");
        ckDtRegistro.setMargin(new java.awt.Insets(0, 0, 0, 0));
        ckDtRegistro.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                ckDtRegistroItemStateChanged(evt);
            }
        });

        ckDtReferencia.setText("Data de Referencia");
        ckDtReferencia.setMargin(new java.awt.Insets(0, 0, 0, 0));
        ckDtReferencia.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                ckDtReferenciaItemStateChanged(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        jLabel1.setText("Valor total:");
        jLabel1.setToolTipText("teste");
        jLabel1.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);

        lblValorTotal.setFont(new java.awt.Font("Calibri", 0, 18)); // NOI18N
        lblValorTotal.setToolTipText("teste");
        lblValorTotal.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel7))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbTipoEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(ftfDtRegistroIni, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(2, 2, 2)
                                .addComponent(imgDataLancamentoIni))
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(ftfDtRegistroFim, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(2, 2, 2)
                                .addComponent(imgDataLancamentoFim))
                            .addComponent(jLabel3)))
                    .addComponent(ckDtRegistro))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(ftfDtReferenciaIni, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(2, 2, 2)
                                .addComponent(imgDataReferenciaIni))
                            .addComponent(jLabel9))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(ftfDtReferenciaFim, javax.swing.GroupLayout.PREFERRED_SIZE, 126, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(2, 2, 2)
                                .addComponent(imgDataReferenciaFim))
                            .addComponent(jLabel10)))
                    .addComponent(ckDtReferencia)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(cbEmissores, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5)
                                .addGap(222, 222, 222)
                                .addComponent(btConsultarEntrada)))
                        .addGap(41, 41, 41)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblValorTotal)))
                .addContainerGap(236, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(ckDtRegistro)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(0, 0, 0)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ftfDtRegistroIni, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(imgDataLancamentoIni, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(0, 0, 0)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ftfDtRegistroFim, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(imgDataLancamentoFim, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(ckDtReferencia)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel9)
                                .addGap(0, 0, 0)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ftfDtReferenciaIni, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(imgDataReferenciaIni, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addGap(0, 0, 0)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ftfDtReferenciaFim, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(imgDataReferenciaFim, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel4)
                                .addGap(0, 0, 0)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(cbTipoEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(cbEmissores, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(btConsultarEntrada, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel1)
                                    .addComponent(lblValorTotal)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel7)
                                .addGap(0, 0, 0)
                                .addComponent(cbUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(22, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addGap(0, 0, Short.MAX_VALUE))))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        txtUsuario.setText("Usuário:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(txtUsuario)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lbUsuario)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUsuario)
                    .addComponent(lbUsuario))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        tbEntradas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "#", "Dt Registro", "Usuario", "Dt Atual.", "Usuario Atual.", "Dt Ref.", "Emissor", "Tipo Entrada", "Valor", "Status", "Descrição"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false, false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbEntradas.getTableHeader().setReorderingAllowed(false);
        tbEntradas.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbEntradasMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbEntradas);
        if (tbEntradas.getColumnModel().getColumnCount() > 0) {
            tbEntradas.getColumnModel().getColumn(0).setMinWidth(30);
            tbEntradas.getColumnModel().getColumn(0).setPreferredWidth(30);
            tbEntradas.getColumnModel().getColumn(0).setMaxWidth(30);
        }

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 302, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(42, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void imgDataLancamentoFimMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imgDataLancamentoFimMouseClicked
        if(ckDtRegistro.isSelected()){
            frmCalendar dialog = new frmCalendar(new javax.swing.JFrame(), false, ftfDtRegistroFim);
            int x = ftfDtRegistroFim.getLocationOnScreen().x;
            int y = ftfDtRegistroFim.getLocationOnScreen().y;
            dialog.setBounds(x, y + ftfDtRegistroFim.getHeight(), 224, 140);
            dialog.setVisible(true);
        }
    }//GEN-LAST:event_imgDataLancamentoFimMouseClicked

    private void imgDataLancamentoIniMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imgDataLancamentoIniMouseClicked
        if(ckDtRegistro.isSelected()){
            frmCalendar dialog = new frmCalendar(new javax.swing.JFrame(), false, ftfDtRegistroIni);
            int x = ftfDtRegistroIni.getLocationOnScreen().x;
            int y = ftfDtRegistroIni.getLocationOnScreen().y;
            dialog.setBounds(x, y + ftfDtRegistroIni.getHeight(), 224, 140);
            dialog.setVisible(true);
        }

    }//GEN-LAST:event_imgDataLancamentoIniMouseClicked

    public void consultarEntrada(){                                                   
        alinharColunasTabelaEvasoes(tbEntradas);
        DefaultTableModel model = (DefaultTableModel) tbEntradas.getModel();
        if(model.getRowCount() > 0){
            while(model.getRowCount() > 0){
                model.removeRow(0);
            }
        }        
        Date d1 = null;
        Date d2 = null;
        Date d3 = null;
        Date d4 = null;
        
        if(ckDtRegistro.isSelected()){
            if(ftfDtRegistroIni.getText().isEmpty()){
                JOptionPane.showMessageDialog(rootPane, "Informe a data de REGISTRO INICIAL!");
                return;
            }
            if(ftfDtRegistroFim.getText().isEmpty()){
                JOptionPane.showMessageDialog(rootPane, "Informe a data de REGISTRO FINAL!");
                return;
            }
            try {
                d1 = new SimpleDateFormat("dd/MM/yyyy").parse(ftfDtRegistroIni.getText());
                d2 = new SimpleDateFormat("dd/MM/yyyy").parse(ftfDtRegistroFim.getText());
                if(d1.compareTo(d2) == 1){
                    JOptionPane.showMessageDialog(rootPane, "Data inicial maior que a final (Data Registro)");
                    return;
                }
            } catch (ParseException ex) {
                Logger.getLogger(IfrmManterEntrada.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(rootPane, ex);
            }
        }
        if(ckDtReferencia.isSelected()){
            if(ftfDtReferenciaIni.getText().isEmpty()){
                JOptionPane.showMessageDialog(rootPane, "Informe a data de REFERÊNCIA INICIAL!");
                return;
            }
            if(ftfDtReferenciaFim.getText().isEmpty()){
                JOptionPane.showMessageDialog(rootPane, "Informe a data de REFERÊNCIA FINAL!");
                return;
            }
            try {
                d3 = new SimpleDateFormat("dd/MM/yyyy").parse(ftfDtReferenciaIni.getText());
                d4 = new SimpleDateFormat("dd/MM/yyyy").parse(ftfDtReferenciaFim.getText());
                if(d3.compareTo(d4) == 1){
                    JOptionPane.showMessageDialog(rootPane, "Data inicial maior que a final (Data Referência)");
                    return;
                }
            } catch (ParseException ex) {
                Logger.getLogger(IfrmManterEntrada.class.getName()).log(Level.SEVERE, null, ex);
                JOptionPane.showMessageDialog(rootPane, ex);
            }
        }

        int idEmissor = 0;
        Iterator<Emissor> itEmissor = emissores.iterator();
        while(itEmissor.hasNext()){
            Emissor emissor = itEmissor.next();
            String nome = emissor.getNome();            
            if (nome.equals(cbEmissores.getSelectedItem())) {
                idEmissor = emissor.getId();
            }
        }
        int idTipoEntrada = 0;
        Iterator<TipoEntrada> itTipoEntrada = tipoEntrada.iterator();
        while (itTipoEntrada.hasNext()) {
            TipoEntrada te = itTipoEntrada.next();
            String nome = te.getNome();            
            if (nome.equals(cbTipoEntrada.getSelectedItem())) {
                idTipoEntrada = te.getId();
            }
        }
        int idUsuario =0;
        Iterator<Usuario> itUsuario = usuarios.iterator();
        while (itUsuario.hasNext()) {
            Usuario u = itUsuario.next();
            String nome = u.getNome();
            if (nome.equals(cbUsuario.getSelectedItem())) {
                idUsuario = u.getId();
            }
        }        
        Double valorTotal = 0.0;
        entradas = dao.listarEntradas(d1, d2, d3, d4, idEmissor, idTipoEntrada, idUsuario);        
        Iterator<Entrada> it = entradas.iterator();          
        while (it.hasNext()) {
            Entrada entrada = it.next();            
            //        entrada.getId();
            String dtRegistro = new SimpleDateFormat("dd/MM/yyyy HH:mm").format(entrada.getDtRegistro());
            //        entrada.getIdUsuario();
            String nomeUsuario = entrada.getNomeUsuario();
            String dtAtualizacao = "Null";
            if(entrada.getDtAtualizacao() != null) dtAtualizacao = new SimpleDateFormat("dd/MM/yyyy").format(entrada.getDtAtualizacao());
            //        entrada.getIdUsuarioAtualizacao();
            String nomeUsuarioAtualizacao = entrada.getNomeUsuarioAtualizacao();
            String dtReferencia = new SimpleDateFormat("dd/MM/yyyy").format(entrada.getDtReferencia());
            //        entrada.getIdEmissor();           
            String nomeEmissor = entrada.getNomeEmissor();
            //        entrada.getIdTipo();
            String nomeTipoEntrada = entrada.getNomeTipoEntrada();
            String valor = String.valueOf(new DecimalFormat("#,###.00").format(entrada.getValor()));
            valorTotal = valorTotal + entrada.getValor();
            String status = entrada.getStatus();
            String descricao = entrada.getDescricao();
            int x = entrada.getIdInterno();
            Object[] dados = {x,dtRegistro,nomeUsuario,dtAtualizacao,nomeUsuarioAtualizacao,dtReferencia,nomeEmissor,nomeTipoEntrada,valor,status,descricao};
            model.addRow(dados);
        }
        lblValorTotal.setText("R$ "+new DecimalFormat("#,###.00").format(valorTotal));
    }                                                      
    
    
    private void btConsultarEntradaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btConsultarEntradaActionPerformed
        consultarEntrada();
    }//GEN-LAST:event_btConsultarEntradaActionPerformed

    private void imgDataReferenciaIniMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imgDataReferenciaIniMouseClicked
        if(ckDtReferencia.isSelected()){
            frmCalendar dialog = new frmCalendar(new javax.swing.JFrame(), false, ftfDtReferenciaIni);
            int x = ftfDtReferenciaIni.getLocationOnScreen().x;
            int y = ftfDtReferenciaIni.getLocationOnScreen().y;
            dialog.setBounds(x, y + ftfDtReferenciaIni.getHeight(), 224, 140);
            dialog.setVisible(true);
        }
    }//GEN-LAST:event_imgDataReferenciaIniMouseClicked

    private void imgDataReferenciaFimMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_imgDataReferenciaFimMouseClicked
        if(ckDtReferencia.isSelected()){        
            frmCalendar dialog = new frmCalendar(new javax.swing.JFrame(), false, ftfDtReferenciaFim);
            int x = ftfDtReferenciaFim.getLocationOnScreen().x;
            int y = ftfDtReferenciaFim.getLocationOnScreen().y;
            dialog.setBounds(x, y + ftfDtReferenciaFim.getHeight(), 224, 140);
            dialog.setVisible(true);
        }
    }//GEN-LAST:event_imgDataReferenciaFimMouseClicked

    private void ckDtRegistroItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_ckDtRegistroItemStateChanged
        if(ckDtRegistro.isSelected()){
            ftfDtRegistroIni.setEnabled(true);
            ftfDtRegistroFim.setEnabled(true);
            imgDataLancamentoIni.setEnabled(true);
            imgDataLancamentoFim.setEnabled(true);
            ftfDtRegistroIni.setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));
            ftfDtRegistroFim.setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));
        } else {
            ftfDtRegistroIni.setEnabled(false);
            ftfDtRegistroFim.setEnabled(false);
            ftfDtRegistroIni.setText("");
            ftfDtRegistroFim.setText("");
            imgDataLancamentoIni.setEnabled(false);
            imgDataLancamentoFim.setEnabled(false);
        }
        
    }//GEN-LAST:event_ckDtRegistroItemStateChanged

    private void ckDtReferenciaItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_ckDtReferenciaItemStateChanged
        if(ckDtReferencia.isSelected()){
            ftfDtReferenciaIni.setEnabled(true);
            ftfDtReferenciaFim.setEnabled(true);
            imgDataReferenciaIni.setEnabled(true);
            imgDataReferenciaFim.setEnabled(true);
            ftfDtReferenciaIni.setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));
            ftfDtReferenciaFim.setText(new SimpleDateFormat("dd/MM/yyyy").format(new Date()));
        } else {
            ftfDtReferenciaIni.setEnabled(false);
            ftfDtReferenciaFim.setEnabled(false);
            ftfDtReferenciaIni.setText("");
            ftfDtReferenciaFim.setText("");
            imgDataReferenciaIni.setEnabled(false);
            imgDataReferenciaFim.setEnabled(false);
        }
    }//GEN-LAST:event_ckDtReferenciaItemStateChanged

    public void selecionarRegistroNaTabela(int n){
        int registros = tbEntradas.getRowCount();
        for (int i = 0; i < registros; i++) {
            System.out.println("N: "+n+" I: "+i);
            if(n == Integer.parseInt(tbEntradas.getValueAt(i, 0).toString())){
                tbEntradas.getSelectionModel().setSelectionInterval(i, i);
                break;
            }
        }        
    }
    
    
    private void tbEntradasMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbEntradasMouseClicked
        if(evt.getClickCount() == 2){
            if(usuario.getPerfil().equals("usuario")) return;            
            int row = tbEntradas.getSelectedRow();            
            int idInterno = new Integer(tbEntradas.getValueAt(row, 0).toString());
            Iterator<Entrada> it = entradas.iterator();
            while (it.hasNext()) {
                Entrada entrada = it.next();                
                if(idInterno == entrada.getIdInterno()){
                    JDialog dg = new FrmAlterarEntrada(null, true, usuario, entrada, this);
                    dg.setVisible(true);                                        
                    dg.setTitle("Altera entrada");
                    dg.setLocationRelativeTo(null);
                }
            }
        }
    }//GEN-LAST:event_tbEntradasMouseClicked

    private void carregaTipoEntradas() throws SQLException{
        Iterator<TipoEntrada> it = tipoEntrada.iterator();
        while(it.hasNext()){
            cbTipoEntrada.addItem(it.next().getNome());
        }        
    }
    private void carregaEmissores() throws SQLException{
        Iterator<Emissor> it = emissores.iterator();
        while(it.hasNext()){
            cbEmissores.addItem(it.next().getNome());
        }
    }
    
    private void carregaUsuarios(){
        Iterator<Usuario> it = usuarios.iterator();
        while (it.hasNext()) {
            cbUsuario.addItem(it.next().getNome());            
        }
    }
    
    private void limpaCampos(){
        cbTipoEntrada.setSelectedIndex(0);
        cbEmissores.setSelectedIndex(0);
        
    }
    
    private void alinharColunasTabelaEvasoes(JTable tabela){

        DefaultTableCellRenderer esquerda = new DefaultTableCellRenderer();  
        DefaultTableCellRenderer centralizado = new DefaultTableCellRenderer();  
        DefaultTableCellRenderer direita = new DefaultTableCellRenderer();  

        esquerda.setHorizontalAlignment(SwingConstants.LEFT);  
        centralizado.setHorizontalAlignment(SwingConstants.CENTER);  
        direita.setHorizontalAlignment(SwingConstants.RIGHT);  

        tabela.getColumnModel().getColumn(0).setCellRenderer(centralizado);
        tabela.getColumnModel().getColumn(1).setCellRenderer(centralizado);  
        tabela.getColumnModel().getColumn(2).setCellRenderer(esquerda);
        tabela.getColumnModel().getColumn(3).setCellRenderer(centralizado);
        tabela.getColumnModel().getColumn(4).setCellRenderer(esquerda);
        tabela.getColumnModel().getColumn(5).setCellRenderer(centralizado);
        tabela.getColumnModel().getColumn(6).setCellRenderer(esquerda);
        tabela.getColumnModel().getColumn(7).setCellRenderer(esquerda);
        tabela.getColumnModel().getColumn(8).setCellRenderer(direita);
        tabela.getColumnModel().getColumn(9).setCellRenderer(esquerda);
        tabela.getColumnModel().getColumn(10).setCellRenderer(esquerda);
        tabela.getColumnModel().getColumn(0).setPreferredWidth(30);
        tabela.getColumnModel().getColumn(1).setPreferredWidth(110);
        tabela.getColumnModel().getColumn(2).setPreferredWidth(110);
        tabela.getColumnModel().getColumn(3).setPreferredWidth(80);
        tabela.getColumnModel().getColumn(4).setPreferredWidth(110);
        tabela.getColumnModel().getColumn(5).setPreferredWidth(80);
        tabela.getColumnModel().getColumn(6).setPreferredWidth(110);
        tabela.getColumnModel().getColumn(7).setPreferredWidth(110);
        tabela.getColumnModel().getColumn(8).setPreferredWidth(60);
        tabela.getColumnModel().getColumn(9).setPreferredWidth(60);
        tabela.getColumnModel().getColumn(10).setPreferredWidth(110);
    }

    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bgSituacao;
    public javax.swing.JButton btConsultarEntrada;
    private javax.swing.JComboBox cbEmissores;
    private javax.swing.JComboBox cbTipoEntrada;
    private javax.swing.JComboBox cbUsuario;
    private javax.swing.JCheckBox ckDtReferencia;
    private javax.swing.JCheckBox ckDtRegistro;
    private javax.swing.JFormattedTextField ftfDtReferenciaFim;
    private javax.swing.JFormattedTextField ftfDtReferenciaIni;
    private javax.swing.JFormattedTextField ftfDtRegistroFim;
    private javax.swing.JFormattedTextField ftfDtRegistroIni;
    private javax.swing.JLabel imgDataLancamentoFim;
    private javax.swing.JLabel imgDataLancamentoIni;
    private javax.swing.JLabel imgDataReferenciaFim;
    private javax.swing.JLabel imgDataReferenciaIni;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbUsuario;
    private javax.swing.JLabel lblValorTotal;
    private javax.swing.JTable tbEntradas;
    private javax.swing.JLabel txtUsuario;
    // End of variables declaration//GEN-END:variables
}
